package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.goods.TbGoods;
import com.dongdongshop.pojo.goods.TbGoodsDesc;
import com.dongdongshop.pojo.item.TbItem;
import com.dongdongshop.pojo.itemCat.TbItemCat;
import com.dongdongshop.service.*;
import com.dongdongshop.service.goods.GoodsDescService;
import com.dongdongshop.service.goods.GoodsService;
import com.dongdongshop.service.item.ItemServiceI;
import com.dongdongshop.service.itemCat.ItemCatService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ItemPageServiceImpl implements ItemPageService {

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Value("${freemarker.pageDir}")
    private String pageDir;

    @Reference
    private GoodsService goodsService;

    @Reference
    private GoodsDescService goodsDescService;

    @Reference
    private ItemCatService itemCatService;

    @Reference
    private ItemServiceI itemService;
    @Override
    public void itemDetail(Long goodsId) {
        //加载配置
        Configuration configuration = freeMarkerConfigurer.getConfiguration();

        FileWriter fileWriter = null;
        //创建模板
        try {
            Template template = configuration.getTemplate("item.ftl");
            //创建数据模型对象
            Map map = new HashMap();

            //查询goods表
            TbGoods goods = goodsService.finGoodsById(goodsId);

            //查询goodsDesc表
            TbGoodsDesc goodsDesc = goodsDescService.finGoodsById(goodsId);

            Long category1Id = goods.getCategory1Id();
            Long category2Id = goods.getCategory2Id();
            Long category3Id = goods.getCategory3Id();

            //查询itemCat表
            TbItemCat itemCat1 = itemCatService.findItemcategory1Id(category1Id);
            TbItemCat itemCat2 = itemCatService.findItemcategory2Id(category2Id);
            TbItemCat itemCat3 = itemCatService.findItemcategory3Id(category3Id);

            //查询sku列表。（item）
            List<TbItem> itemList = itemService.selectItemList(goods.getId());

            map.put("goods",goods);
            map.put("goodsDesc",goodsDesc);
            map.put("itemCat1",itemCat1);
            map.put("itemCat2",itemCat2);
            map.put("itemCat3",itemCat3);
            map.put("itemList",itemList);

            //创建输出流对象
            fileWriter = new FileWriter(pageDir+goodsId+".html");
            //使用模板输出
            template.process(map,fileWriter);

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (fileWriter != null){
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
